from fastapi import APIRouter, HTTPException
from typing import Dict, Any
from src.core.download_manager import DownloadManager
from src.models.download_models import DownloadRequest
from src.infrastructure.log_decorators import log_operation
import logging

logger = logging.getLogger(__name__)
router = APIRouter()

download_manager = DownloadManager()

@router.post("/")
@log_operation("download")
async def start_download(request: Dict[str, Any]):
    """开始下载任务"""
    try:
        # 创建下载任务
        task_id = await download_manager.create_task(request)
        return {"task_id": task_id}
    except Exception as e:
        logger.error(f"Failed to start download: {str(e)}")
        raise HTTPException(status_code=400, detail=str(e))


@router.post("/batch")
async def start_batch_download(request: Dict[str, Any]):
    """开始批量下载任务"""
    try:
        task_ids = await download_manager.create_batch_tasks(request)
        return {"task_ids": task_ids}
    except Exception as e:
        logger.error(f"Failed to start batch download: {str(e)}")
        raise HTTPException(status_code=400, detail=str(e)) 